package com.ly.mp.cmc.common.exception;

import com.ly.mp.cmc.common.reponse.BaseResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: ly-songpengfei
 * @Date: 2025/8/4 16:38
 * @Description: 全局异常处理类
 * @Version: 1.0
 */
@RestControllerAdvice
/** 统一异常处理注解（ControllerAdvice + ResponseBody）*/
public class GlobalExceptionHandler {

    // 处理业务异常
    @ExceptionHandler(value = BusinessException.class) // 处理异常类型
    public BaseResult defaultExceptionHandler(HttpServletRequest request, HttpServletResponse response,
                                              BusinessException businessException) {
        BaseResult result = new BaseResult(businessException.getCode(), businessException.getMessage());
        return result;
    }

    // 处理系统异常
    @ExceptionHandler(value = Exception.class)
    public BaseResult defaultExceptionHandler(HttpServletRequest request, HttpServletResponse response,
                                              Exception e) {
        BaseResult result = new BaseResult(ExceptionEnum.SYSTEM_EXCEPTION.getCode(),
                ExceptionEnum.SYSTEM_EXCEPTION.getMessage());
        return result;
    }

}
